﻿//-----------------------------------------------------------------------------------------------------------
// WBFSSync Project by Omega Frost 
// http://wbfssync.codeplex.com/
//
// WBFSSync is Licensed under the terms of the 
// Microsoft Reciprocal License (Ms-RL)
//-----------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WbfsSync.Controls
{
    //-------------------------------------------------------------------------------------------------------
    // Interaction logic for SearchBox.xaml
    //-------------------------------------------------------------------------------------------------------
    public partial class SearchBox : UserControl
    {
        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        protected SolidColorBrush inactiveBack;
        protected SolidColorBrush activeBack;

        protected Boolean focused;


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public event EventHandler SearchQueryChanged;


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public String SearchMessage
        {
            get { return this.searchText.Text; }
            set { this.searchText.Text = value; }
        }

        public String SearchText
        {
            get { return this.searchQuery.Text; }
            set { SetSearchText(value); }
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public SearchBox()
        {
            this.focused = false;
            this.inactiveBack = new SolidColorBrush(Color.FromArgb(0xC0, 0xF0, 0xF0, 0xF0));
            this.activeBack = new SolidColorBrush(Color.FromArgb(0xE6, 0xF8, 0xF8, 0xF8));
            
            InitializeComponent();

            this.RectangleBack1.Fill = this.inactiveBack;

            this.searchQuery.TextChanged += new TextChangedEventHandler(searchQuery_TextChanged);
            this.searchButton.Click += new RoutedEventHandler(searchButton_Click);
            this.MouseEnter += new MouseEventHandler(WSSearchBox_MouseEnter);
            this.MouseLeave += new MouseEventHandler(WSSearchBox_MouseLeave);
            this.GotFocus += new RoutedEventHandler(WSSearchBox_GotFocus);
            this.LostFocus += new RoutedEventHandler(WSSearchBox_LostFocus);

        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        void searchButton_Click(object sender, RoutedEventArgs e)
        {
            this.searchQuery.Text = String.Empty;
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        void searchQuery_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (String.IsNullOrEmpty(this.searchQuery.Text))
                this.searchButton.IsEnabled = false;
            else
                this.searchButton.IsEnabled = true;

            if (this.SearchQueryChanged != null)
                this.SearchQueryChanged(this, EventArgs.Empty);
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        private void SetSearchText(string value)
        {
            this.searchQuery.Text = value;

            if (string.IsNullOrEmpty(value))
                this.searchText.Visibility = System.Windows.Visibility.Visible;
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        void WSSearchBox_LostFocus(object sender, RoutedEventArgs e)
        {
            this.focused = false;

            if (this.IsMouseOver) this.RectangleBack1.Fill = this.activeBack;
            else this.RectangleBack1.Fill = this.inactiveBack;

            if (String.IsNullOrEmpty(this.searchQuery.Text))
                this.searchQuery.Visibility = System.Windows.Visibility.Visible;
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        void WSSearchBox_GotFocus(object sender, RoutedEventArgs e)
        {
            this.focused = true;
            this.RectangleBack1.Fill = Brushes.White;
            this.searchText.Visibility = System.Windows.Visibility.Hidden;
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        void WSSearchBox_MouseLeave(object sender, MouseEventArgs e)
        {
            if (!this.focused)
                this.RectangleBack1.Fill = this.inactiveBack;
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        void WSSearchBox_MouseEnter(object sender, MouseEventArgs e)
        {
            if (!this.focused)
                this.RectangleBack1.Fill = this.activeBack;
        }
    }
}
